#ifndef _APPS_BWM_SCENERENDERER_CCUBERENDERER_H_
#define _APPS_BWM_SCENERENDERER_CCUBERENDERER_H_

#include "cscenerenderer.h"
#include "sshaperecovery.h"

#include <buola/gui/x11/cglx.h>

#include <buola/datetime/clocks.h>
#include <buola/gl/cprogram.h>
#include <buola/gl/ctexture.h>
#include <buola/gl/cvbo.h>

namespace buola { namespace bwm {

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
////////////////// CCubeRenderer //////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

class CCubeRenderer : public CSceneRenderer
{
///////////////////////////////
/////////// methods ///////////
///////////////////////////////

public:
//////////////
// creation
//
    CCubeRenderer();
    ~CCubeRenderer();

public:
    virtual void Enable();
    virtual void Reshape(Window pOverlay,const CSize_d &pSize);
//painting
    virtual void AnimateDesktopSwitch(int pFrom,int pTo);
    virtual void DragStep(gui::EDragState pType,const CPoint_d &pDelta);
    virtual void DragFinish(gui::EDragState pType);

protected:
/////////////////
// perspective
//
    void StartHRotate(double pDest,const chrono::microseconds &pTime,bool pInc);
    void StartVRotate(double pDest,const chrono::microseconds &pTime,bool pInc);
    void StartXYTranslate(double pXDest,double pYDest,
                const chrono::microseconds &pTime,bool pInc,bool pZoomMult=false);
    void StartZoom(double pDest,const chrono::microseconds &pTime,bool pMult);
    void StartRecovery();

/////////////////////
// composite actions
//
    void DoRecovery(const CMonotonicTime&);
    void Transform(int pDesktop,bool pSpace);

public:
    virtual void Render();
    virtual void OnLoop();

////////////////////////////
/////////// data ///////////
////////////////////////////

///////////////////////
// event and clients
//
protected:
    double mHAngle;
    double mVAngle;
    double mXTranslate;
    double mYTranslate;
    double mZoom;

    SShapeRecovery *mHAngleRec;
    SShapeRecovery *mVAngleRec;
    SShapeRecovery *mXTranslateRec;
    SShapeRecovery *mYTranslateRec;
    SShapeRecovery *mZoomRec;

    mat::CMat4d mModelView;
    mat::CMat4d mProjectionMatrix;
    
    static gl::CShader mVertexShader,mColorShader,mTextureShader;
    static gl::CProgram mProgram,mTexProgram;
    static gl::CUniform mUniformMV;
    static gl::CUniform mUniformMVP;
    
    gl::CTexture mFPSTexture;
    
    gl::CVBO mVBO[4];
    
    GLuint mVertexArray;
};

/*namespace bwm*/ } /*namespace buola*/ }

#endif
